{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 相位之后正弦波的互相关\n",
    "\n",
    "本例展示了如何使用互相关学列来估计两个正弦波的相位之滞后，在理论上两个同频率正弦信号的互相关也以相同的频率震荡。由于样本互相关序列在较大滞后时使用的样本越来越少，样本互相关序列也以相同的频率振荡，但振幅随着滞后的增加而衰减。\n",
    "\n",
    "创建两个频率为2π/10 rad/sample的正弦波。其中一个正弦波的起始相位为0，另一个正弦波的起始相位为-π。在相位滞后为π的信号中添加N（0，0.25^2）的白噪声。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = np.linspace(0,99,100)\n",
    "x = np.cos(2*np.pi*1/10*t)\n",
    "y = np.cos(2*np.pi*1/10*t-np.pi)+0.25*np.random.randn(len(t))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "得到正弦波两个周期(10个样本)的样本互相关序列。绘制互相关序列，标记两个正弦波之间的已知滞后(5个样本)。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x2253676d2d0>]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY0AAAD8CAYAAACLrvgBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAatklEQVR4nO3df5Ac5X3n8ffnhIS3cOKVkApLC8bCVglwlNPiDTannIsDgWQuhTaK44i7K8sOLjl35u5yrghLRRV2kbgkokpI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TEkff/WGwekDrpY0M2luuTopaxpJy4EbgWsj4vU6dbIcIw1V1Yf223U+P8t5oRmWAv8YEYdqLWzm/hznPNSY47PRPfs57wq4hsqdAM9SGVUX4BYqBz/Au6g0YQwCPwYuaHJ8v0nlku9JYG/yugb4A+APkjo3AE9TuctjN/BvWrAfL0g+/4kklrF9mY5TVP5Y1rPAPqCnRd/5WVSSwHtSZS3fn1SS2BHgOJV23+up9J89BBwAHgRmJXV7gDtS6/5+cowOAp9pQZyDVNqtx47RsTsO5wE7xjtGmhznPcmx9ySVE97c6jiT+XecF5oZZ1L+9bFjMlW3JftznPNQQ45PDyNiZmaZlbl5yszMSsZJw8zMMnPSMDOzzJw0zMwsMycNMzPLzEnDzMwyc9IwM7PM/j9GogqIezHrOgAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "corr = np.correlate(x,y,'same')\n",
    "corr /= np.max(corr)\n",
    "plt.stem(corr[60:81])\n",
    "plt.xlim([0,20])\n",
    "plt.ylim([-1,1])\n",
    "plt.plot([5,5],[-1,1],'r')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到，交叉相关序列在滞后5时如预期的那样达到峰值，并以10个样本的周期振荡。"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "c8b08d2241c8157cc559a694afc7fe14d3ec73f4c204ededcfdcbff38dcf9d20"
  },
  "kernelspec": {
   "display_name": "Python 3.10.3 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
